package
{
	/**
	 * 
	 * @author Polar
	 * @date 2012-12-30
	 * @function 
	 * 
	 */
	import starling.events.Event;
	
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.text.TextField;
	
	public class CustomSprite extends Sprite
	{
		private var _quad:Quad;
		private var _signature:TextField;
		
		private var _quadWidth:Number;
		private var _quadHeight:Number;
		
		
		private var _r:Number=0;
		private var _g:Number=0;
		private var _b:Number=0;
		
		
		private var _rDest:Number;
		private var _gDest:Number;
		private var _bDest:Number;
		
		public function CustomSprite(width:Number,height:Number,color:uint = 0xffffff)
		{
			super();
			resetColors();
			_quadWidth = width;
			_quadHeight = height;
			addEventListener(Event.ADDED_TO_STAGE,activate);
		}
		private function activate(e:Event):void
		{
			_quad = new Quad(_quadWidth,_quadHeight);
			_signature = new TextField(100,20,"HelloStarling");
			
			addChild(_quad);
			addChild(_signature);
			
			pivotX = _quadWidth>>1;
			pivotY = _quadHeight>>1;
		}
		private function resetColors():void
		{
			_rDest = Math.random()*255;
			_gDest = Math.random()*255;
			_bDest = Math.random()*255;			
		}
		public function update():void
		{
			_r -= (_r-_rDest)*.01;
			_g -= (_g-_gDest)*.01;
			_b -= (_b-_bDest)*.01;
			
			_quad.color = _r<<16 | _g<<8 | _b;
			
			// when reaching the color, pick another one
			if ( Math.abs( _r - _rDest) < 1 && Math.abs( _g - _gDest) < 1 && Math.abs( _b
				- _bDest) )
				resetColors();
			
			rotation += 0.01;
		}
	}
}